package fastwave.cloud.demo.fastwave.biz.publisher.service.impl;

import com.alibaba.fastjson.JSON;
import fastwave.cloud.demo.fastwave.biz.publisher.config.TransConfig;
import fastwave.cloud.demo.fastwave.biz.publisher.domain.MsgLog;
import fastwave.cloud.demo.fastwave.biz.publisher.domain.Trans;
import fastwave.cloud.demo.fastwave.biz.publisher.mapper.MsgLogMapper;
import fastwave.cloud.demo.fastwave.biz.publisher.mapper.TransMapper;
import fastwave.cloud.demo.fastwave.biz.publisher.service.TransferMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fastwave.cloud.demo.fastwave.biz.publisher.domain.Account;
import fastwave.cloud.demo.fastwave.biz.publisher.mapper.AccountMapper;
import fastwave.cloud.demo.fastwave.biz.publisher.service.AccountService;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>Title: AccountServiceImpl</p>
 *
 * @author: dwLiu
 * @date: 2020/4/11 21:19
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private MsgLogMapper msgLogMapper;
    @Autowired
    private TransMapper transMapper;
    @Autowired
    private TransferMQService rabbitMQService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String transfer(Map<String, Object> param) {
        try {
            Integer id1 = Integer.parseInt(param.get("id1").toString());
            Integer id2 = Integer.parseInt(param.get("id2").toString());
            Float amount = Float.parseFloat(param.get("amount").toString());
            String transId = UUID.randomUUID().toString();
            param.put("transId", transId);

            //1.扣减
            Account account1 = accountMapper.selectById(id1);
            account1.setBalance(account1.getBalance() - amount);
            accountMapper.updateById(account1);

            //增加
//            Account account2 = accountMapper.selectById(id2);
//            account1.setBalance(account2.getBalance() + amount);
//            accountMapper.updateById(account2);

            //2.流水日志
            Trans trans = new Trans();
            trans.setId(transId);
            String message = JSON.toJSONString(param);
            trans.setMessage(message);
            trans.setStatus(1);
            transMapper.insert(trans);

            //3.保存事务消息，防止发送失败后，可以在作业中重试，另外还可以手工处理
            MsgLog msgLog = new MsgLog();
            Date curTime = new Date();
            msgLog.setMsgId(transId);
            msgLog.setMsg(message);
            msgLog.setCreateTime(curTime);
            msgLog.setUpdateTime(curTime);
            msgLog.setExchange(TransConfig.TRANSFER_EXCHANGE_NAME);
            msgLog.setNextTryTime(curTime);
            msgLog.setRoutingKey(TransConfig.TRANSFER_ROUTING_KEY);
            msgLog.setStatus(-1);
            msgLog.setTryCount(0);
            msgLogMapper.insert(msgLog);

            rabbitMQService.sendMessage(param);

            return "转账成功";
        } catch (Exception e) {
            return "转账失败";
        }
    }
}
